/*
 * Copyright (c) $today.year-Now http://www.zxpnet.com All rights reserved.
 */

package com.zxp.springboot.controller;

import com.google.common.base.CaseFormat;
import com.zxp.springboot.dto.Db;
import com.zxp.springboot.pojo.TableClass;
import com.zxp.springboot.service.GenerateService;
import com.zxp.springboot.util.DbUtil;
import io.renren.common.api.R;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @desc:
 * @author: shollin
 * @date: 2021/6/15/015 19:48
 */
@RestController
@RequiredArgsConstructor
public class IndexController {

    private final GenerateService generateService;

    /**
     * 初始化连接
     * @param db
     * @return
     */
    @RequestMapping("/connect")
    public R connect(@Validated @RequestBody Db db){

        Connection conn = null;
        try {
            conn = DbUtil.initDB(db);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        } catch (SQLException se) {
            se.printStackTrace();
            return R.fail(se.getMessage());
        }

        if (conn == null) {
            return R.fail("数据库连接失败");
        }

        return R.success("数据库连接成功");
    }

    @PostMapping("/config")
    public R config(@RequestBody Map<String, String> map) {
        String packageName = map.get("packageName");
        try {
            Connection connection = DbUtil.getConnection();
            DatabaseMetaData metaData = connection.getMetaData();
            ResultSet tables = metaData.getTables(connection.getCatalog(), null, null, null);
            List<TableClass> tableClassList = new ArrayList<>();
            while (tables.next()) {
                TableClass tableClass = new TableClass();
                tableClass.setPackageName(packageName);
                String table_name = tables.getString("TABLE_NAME");
                String modelName = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, table_name);
                tableClass.setTableName(table_name);
                tableClass.setModelName(modelName);
                tableClass.setControllerName(modelName + "Controller");
                tableClass.setMapperName(modelName + "Mapper");
                tableClass.setServiceName(modelName+"Service");
                tableClassList.add(tableClass);
            }
            return R.data(tableClassList).success();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.fail("数据库信息读取失败");
    }

    @PostMapping("/generateCode")
    public R generateCode(@RequestBody List<TableClass> tableData, HttpServletRequest request){

        return generateService.generateCode(tableData,request.getServletContext().getRealPath("/") );

    }

    @RequestMapping("/index")
    public String index(){
        return "index";
    }
}
